﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace 天离安卓权限维持系统.Services
{
    /// <summary>
    /// 获取IP地址归属
    /// </summary>
    public class GetIPInfo
    {
        public static async Task GetIpInfoAsync(string ipAddress)
        {
            string url = "http://ip-api.com/json/" + ipAddress;

            using (HttpClient client = new HttpClient())
            {
                try
                {
                    string response = await client.GetStringAsync(url);
                    JObject ipInfo = JObject.Parse(response);

                    // 打印相关信息
                    Console.WriteLine($"IP: {ipInfo["query"]}");
                    Console.WriteLine($"城市: {ipInfo["city"]}");
                    Console.WriteLine($"国家: {ipInfo["countryCode"]}");
                    string apiinfo = ($"{ipInfo["countryCode"]}") + " / " + ($"{ipInfo["city"]}");
                    Api.Public_Api.IpCity = apiinfo;

                    //return 
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"请求失败: {ex.Message}");
                }
            }
        }
    }
}
